package com.snake.springboot.authority.service.impl;

import com.snake.springboot.authority.api.model.AuthStrategyMenuModel;
import com.snake.springboot.authority.api.request.AuthStrategyMenuQuery;
import com.snake.springboot.authority.api.request.AuthStrategyMenuSave;
import com.snake.springboot.authority.api.response.AuthMenuRsp;
import com.snake.springboot.authority.api.service.AuthStrategyMenuService;
import com.snake.springboot.authority.dao.mybatis.entity.AuthMenuTreeDO;
import com.snake.springboot.authority.dao.mybatis.entity.AuthStrategyMenuDO;
import com.snake.springboot.authority.dao.mybatis.mappers.AuthStrategyMenuMapper;
import com.snake.springboot.authority.service.assembler.AuthStrategyMenuAssembler;
import org.snake.common.utils.CollectionUtils;
import org.snake.common.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类 {@code AuthStrategyMenuServiceImpl}  <br> .
 * <p>权限策略与菜单关系实现类
 * <p>创建时间：2025/02/08
 *
 * @author Huang.yj
 */
@Service
public class AuthStrategyMenuServiceImpl implements AuthStrategyMenuService {
    @Autowired
    private AuthStrategyMenuMapper authStrategyMenuMapper;

    @Override
    public List<AuthStrategyMenuModel> queryAuthStrategyMenuRef(AuthStrategyMenuModel model) {
        AuthStrategyMenuDO condition = AuthStrategyMenuAssembler.assembleAuthStrategyMenuDO(model);
        List<AuthStrategyMenuDO> authStrategyMenuDOS = authStrategyMenuMapper.findByCondition(condition);
        List<AuthStrategyMenuModel> result = ListUtils.copy(authStrategyMenuDOS,AuthStrategyMenuModel.class);
        return result;
    }

    @Override
    public int saveStrategyMenuRef(AuthStrategyMenuSave params) {
        int result = 0;
        List<AuthStrategyMenuDO> records = AuthStrategyMenuAssembler.createAuthStrategyMenuDOList(params);
        authStrategyMenuMapper.delete(new AuthStrategyMenuDO(params.getStrategyId(), params.getSystemId(),null));
        if(!CollectionUtils.isNullOrEmpty(records)){
            result = authStrategyMenuMapper.insertBatch(records);
        }
        return result;
    }

    @Override
    public List<AuthMenuRsp> queryAuthMenuTree(AuthStrategyMenuQuery params) {
        List<AuthMenuTreeDO> list = authStrategyMenuMapper.findMenuTree(params.getStrategyId(), params.getSystemId());
        List<AuthMenuRsp> result = ListUtils.copy(list, AuthMenuRsp.class);
        return result;
    }


}
